﻿using AIStudio.Core.Models;
using AIStudio.Wpf.Business.IBussiness;
using AIStudio.Wpf.Entity.Models;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Accelerider.Extensions.Mvvm;
using AIStudio.Wpf.Home.Views;

namespace AIStudio.Wpf.Home.ViewModels
{
    class MainViewModel: BindableBase, IViewLoadedAndUnloadedAware
    {
        IContainerExtension _container;
        IRegionManager _regionManager;
        IOperator _operator;
        IDataProvider _dataProvider;

        public MainViewModel(IContainerExtension container, IRegionManager regionManager, IOperator ioperator, IDataProvider dataProvider)
        {
            _container = container;
            _regionManager = regionManager;
            _operator = ioperator;
            _dataProvider = dataProvider;
        }


        public void OnLoaded()
        {
            InitMenu();
        }

        public void OnUnloaded()
        {
      
        }

        private ObservableCollection<AMenuItem> _menuItems = new ObservableCollection<AMenuItem>();
        public ObservableCollection<AMenuItem> MenuItems
        {
            get { return _menuItems; }
            set
            {
                SetProperty(ref _menuItems, value);
            }
        }


        private void Menu(AMenuItem menuItem)
        {
            NavigationParameters paras = new NavigationParameters();
            paras.Add("Title", menuItem.Label);
            _regionManager.RequestNavigate("MainContentRegion", menuItem.WpfCode, NavigationComplete,  paras);
        }

        private void NavigationComplete(NavigationResult result)
        {
            if (result.Result == false)
            {
                MainView.Snackbar.MessageQueue?.Enqueue($"{result.Context.Uri.ToString()}打开失败");
            }
        }

        private async void InitMenu()
        {
            var userinfo = await _dataProvider.GetData<UserInfoPermissions>("/Base_Manage/Home/GetOperatorInfo");
            if (!userinfo.Success)
            {
                throw new System.Exception(userinfo.Msg);
            }

            _operator.Property = userinfo.Data.UserInfo;
            _operator.Permissions = userinfo.Data.Permissions;
            _operator.Avatar = userinfo.Data.UserInfo.Avatar;

            var menuinfo = await _dataProvider.GetData<List<Base_ActionTree>>("/Base_Manage/Home/GetOperatorMenuList");
            if (!menuinfo.Success)
            {
                throw new System.Exception(menuinfo.Msg);
            }
            BuildMenu(menuinfo.Data);
        }

        private void BuildMenu(List<Base_ActionTree> base_Actions)
        {
            var nodes = base_Actions.Where(p => string.IsNullOrEmpty(p.ParentId));
            foreach (var node in nodes)
            {
                AMenuItem aMenuItem = new AMenuItem() { Glyph = node.Icon, Label = node.Text, Code = node.Url, Type = node.Type, ParentId = node.ParentId, Id = node.Id };
                MenuItems.Add(aMenuItem);
                if (aMenuItem.Type == 1)
                {
                    aMenuItem.Command = new DelegateCommand<AMenuItem>(para => this.Menu(para));
                }
                SubBuildMenu(aMenuItem, node, aMenuItem.Id);
            }
        }

        private void SubBuildMenu(AMenuItem menuItem, Base_ActionTree parent, string parentid)
        {
            if (parent.Children != null)
            {
                foreach (var node in parent.Children)
                {
                    AMenuItem aMenuItem = new AMenuItem() { Glyph = node.Icon, Label = node.Text, Code = node.Url, Type = node.Type, ParentId = node.ParentId, Id = node.Id };
                    menuItem.AddChildren(aMenuItem);
                    if (aMenuItem.Type == 1)
                    {
                        aMenuItem.Command = new DelegateCommand<AMenuItem>(para => this.Menu(para));
                    }
                    SubBuildMenu(aMenuItem, node, aMenuItem.Id);
                }
            }
        }


    }
}
